/**
 * Copyright (c) 2012-2013 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners.
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.amaze.ui;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;

import com.nokia.example.amaze.Main;

/**
 * A dialog for showing information about this application.
 */
public class InfoDialog {
    // Constants
    public static final String LINK_URL = "http://developer.nokia.com";
    private static final int TEXT_WIDTH = 240 - MazeCanvas.MARGIN * 2;

    // Members
    private Image _backgroundImage = null;
    private Font _infoTextFont = null;
    private TextWrapper _textWrapper = null;
    private MIDlet _midlet = null;
    private final String _infoText
        = "This is a Nokia example game application which demonstrates the use "
        + "of Mobile 3D Graphics framework with accelerometer sensor and "
        + "gestures. Guide the marble through the maze by tilting the phone "
        + "before the time runs out.\n\n"
        + "For more information, visit:";
    private int _linkTextY = 0;
    private int _linkTextHeight = 0;

    /**
     * Constructor.
     */
    public InfoDialog(MIDlet midlet) {
        _midlet = midlet;
        _backgroundImage = Main.makeImage("/graphics/dimmed-bg.png");
        
        _infoTextFont = Font.getFont(
            Font.getDefaultFont().getFace(),
            Font.getDefaultFont().getStyle(),
            Main.HAS_ONE_KEY_BACK ? Font.SIZE_SMALL : Font.SIZE_MEDIUM); 
        
        _textWrapper = TextWrapper.instance(_infoTextFont, TEXT_WIDTH);
    }

    /**
     * Paints the info dialog.
     * @param graphics
     */
    public void paint(Graphics graphics) {
        final int topLeft = Graphics.TOP | Graphics.LEFT;
        
        graphics.drawImage(_backgroundImage, 0, 0, topLeft);
        graphics.setColor(MazeCanvas.TEXT_COLOR);
        
        int y = MazeCanvas.MARGIN + 20;
        
        final Font titleFont = Font.getFont(
            Font.getDefaultFont().getFace(),
            Font.STYLE_BOLD,
            Font.SIZE_LARGE);
        graphics.setFont(titleFont);
        graphics.drawString("aMaze " + _midlet.getAppProperty("MIDlet-Version"),
                            MazeCanvas.MARGIN, y, topLeft);
        
        graphics.setFont(_infoTextFont);
        _textWrapper.setWidth(TEXT_WIDTH);
        
        try {
            _textWrapper.setText(_infoText);
            y += MazeCanvas.MARGIN * 2 + 20;
            
            while (_textWrapper.hasMoreLines()) {
                graphics.drawString(_textWrapper.nextLine(),
                        MazeCanvas.MARGIN, y, topLeft);
                y += _textWrapper.lineHeight();
            }
        }
        catch (IllegalArgumentException e) {}
        
        _linkTextY = y + MazeCanvas.MARGIN;
        
        final Font linkFont = Font.getFont(
            _infoTextFont.getFace(),
            Font.STYLE_UNDERLINED,
            _infoTextFont.getSize());
        _linkTextHeight = linkFont.getHeight();
        graphics.setFont(linkFont);
        graphics.drawString(LINK_URL, MazeCanvas.MARGIN, _linkTextY, topLeft);
    }

    /** 
     * @return The Y coordinate of the top of the link text.
     */
    public int linkTextY() {
        return _linkTextY;
    }

    /** 
     * @return The height of the link text in pixels.
     */
    public int linkTextHeight() {
        return _linkTextHeight;
    }
}
